package tech.yangzhijie.basic.java04;

public class Java0503 {
    public static void main(String[] args) {

        Point point1 = new Point(1,1);
        Point point2 = new Point(3,3);
        Line line1 = new Line(point1,point2);
        System.out.printf("直线%s的斜率为%s\n",line1,line1.slope());

        Point point3 = new Point(2,5);
        System.out.printf("点%s %s 直线%s上\n",point3,line1.whetherThePointIsOnAStraightLine(point3)?"在":"不在",line1);

        System.out.printf("点%s到直线%s的距离为%s\n",point3,line1,line1.givenTheDistanceFromThePointToTheLine(point3));
        Point point4 = new Point(4,4);
        System.out.printf("点%s到直线%s的距离为%s\n",point4,line1,line1.givenTheDistanceFromThePointToTheLine(point4));
    }
}

class Line{
    private double a;
    private double b;
    private double c;

    public Line(Point point1, Point point2) {
        this.a = point1.getX() - point2.getX();
        this.b = point2.getY() - point1.getY();
        this.c = point2.getX()*point1.getY() - point1.getX()*point2.getY();
    }

    public Line() {
    }

    public double slope(){
        return -a/b;
    }

    public boolean whetherThePointIsOnAStraightLine(Point point){
        if(a*point.getX()+b*point.getY()+c == 0){
            return true;
        }
        return false;
    }

    public double givenTheDistanceFromThePointToTheLine(Point point){
        return Math.abs((a*point.getX()+b*point.getY()+c)/Math.sqrt(a*a+b*b));
    }

    @Override
    public String toString() {
        return String.format("(%sx+%sy+%s)",a,b,c);
    }
}
